<?php
    require_once(dirname(dirname(dirname(__FILE__))).'/core.php');
?><!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>个人信息</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="full-screen" content="yes"/>
    <meta name="browsermode" content="application"/>
    <!-- QQ强制竖屏 QQ强制全屏 -->
    <meta name="x5-orientation" content="portrait"/>
    <meta name="x5-fullscreen" content="true"/>
    <meta name="x5-page-mode" content="app"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" type="text/css" href="../../css/reset.css">
    <link rel="stylesheet" type="text/css" href="../../css/main.css">
	<link rel="stylesheet" type="text/css" href="../../css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="../../css/member.css">
    <script src='/wap/js/layer.m/layer.m.js'></script>
    <script type="text/javascript" src="../../js/config.js"></script>
    <script type="text/javascript" src="../../js/zepto.min.js"></script>
    <script type="text/javascript" src="../../js/common.js"></script>
</head>
<body>
    <header id="header"></header>

    <?php
        $minfo = get_mobile_member_info();
        if(!$minfo){
            header('location:/wap/member/login.html');
            exit;
        }

        if(count($_POST)){
            $data = $_POST;
            foreach($data as $key => &$val){
                $val = (trim($val)) ;
            }
            $save = function() use ($data,&$minfo){

                if($data['member_mobile'] != '' && $data['mobile_code'] != ''){
                    if(!check_mobile_code($data['member_mobile'],$data['mobile_code'])){
    ?>
    <script>
        layermsg('手机号码校验错误！');
    </script>
    <?php
                        return;
                    }
                }

                unset($data['mobile_code']);
                $data['member_mobile_bind'] = 1;

                //处理头像
                if(!empty($_POST['member_avatar'])){
                    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $_POST['member_avatar'], $result)){
                        $type = $result[2];
                        $new_file = dirname(BASE_PATH) . "/data/upload/shop/avatar/avatar_{$minfo['member_id']}.jpg";
                        if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $_POST['member_avatar'])))){
//                            echo '新文件保存成功：', $new_file;
                            $_POST['member_avatar'] = "avatar_{$minfo['member_id']}.jpg";
                        }
                        else{
                            unset($_POST['member_avatar']);
                        }

                    }
                }

                $ret = Model('member')->where(array('member_id' => $minfo['member_id']))->update($data);
                if($ret){
    ?>
    <script>
        layermsg('保存成功',function(){
            location.href = "/wap/tmpl/member/member.php";
        });
    </script>
    <?php                    
                }
                $minfo = get_mobile_member_info();
            };
            $save();

        }
    ?>
    <style>
        #info{
            padding: 10px;
        }
        #info ul li{
            padding-top: 0.5em;
            padding-bottom: 0.5em;
        }
        #info input[type=text]{
            width : 95%;
            padding : 0.5em 0;
            display: block;
            line-height: 1em;
            text-indent: 0.5em;
        }

        #info li {
            overflow: hidden;
        }

        #info li span{
            display: block;
            float: left;
            width: 30%;
            line-height: 2em;
        }

        #info li label{
            display: block;
            float: right;
            width: 66%;
        }
    </style>
    <div id='info'>
        <div style="height: 30px;line-height: 30px;color:#D9434E;font-size: 16px;">
                个人信息仅用于购买保税商品时向海关推送。
        </div>
        <form method='post'>
        <ul>

            <li>
                <span>
                    用户名：
                </span>
                <label for="">
                    
                    <input type="text" placeholder="" disabled value='<?php echo $minfo['member_name']?>'>
                </label>
            </li>
            <li>
                <span>
                    手机号：
                </span>
                <label for="">
                    <input type="text" placeholder="" name='member_mobile' value='<?php echo $minfo['member_mobile']?>'>
                    
                </label>
            </li>
            <li style='display: none;' id='captcha'>
                <span>手机校验码：</span> 
                <label for="">
                    <input type="text" name='mobile_code' value=''>
                </label>
            </li>
            <li>
                <span>
                    真实姓名：
                </span>
                <label for="">
                    <input type="text" placeholder="" name='member_truename' value='<?php echo $minfo['member_truename']?>'>
                    
                </label>
            </li>
            <li>
                <span>
                    身份证号码：
                </span>
                <label for="">
                    <input type="text" placeholder="" name='idcard' value='<?php echo $minfo['idcard']?>'>
                </label>
            </li>
            <li>
                <span>
<!--                    身份证号码信息提示-->
                </span>
                <label>
                    提示：请务必保证身份证和姓名的一致性，因填写错误导致海关查验失败的损失需自行负责。
                </label>
            </li>
            <li>
                <span>
                    头像：
                </span>
                <div style='clear:both'>
                    <input type="file" placeholder="" data-bind='member_avatar' accept="image/*">
                    <input type="hidden" name='member_avatar' value='<?php echo $minfo[member_avatar]?>'>
                </div>
                <div style="margin-top: 10px;">
                    <img src="<?php echo getMemberAvatarForID($minfo['member_id'])?>" alt="" data-bind='zpic' style='max-height: 80px;'>
                </div>
            </li>
            <li>
                <span>
                    身份证正面：
                </span>
                <div style='clear:both'>
                    <input type="file" placeholder="" data-bind='zpic' accept="image/*">
                    <input type="hidden" name='zpic' value='<?php echo $minfo[zpic]?>'>
                </div>


                <div style="margin-top: 10px;">
                    <?php if(!empty($minfo['zpic'])){?>
                    <img src="<?php echo $minfo[zpic]?>" alt="" data-bind='zpic' style='max-height: 80px;'>
                    <?php }else{?>
                    <img src="<?php echo $minfo[zpic]?>" alt="" data-bind='zpic' style='max-height: 80px;display:none'>
                    <?php }?>
                </div>
            </li>
            <li>
                <span>
                    身份证反面：
                </span>
                <div style='clear:both'>
                    <input type="file" placeholder="" data-bind='fpic' accept="image/*">
                    <input type="hidden" name='fpic' value='<?php echo $minfo[fpic]?>'>
                </div>
                <div style="margin-top: 10px;">
                    <?php if(!empty($minfo['fpic'])){?>
                    <img src="<?php echo $minfo[fpic]?>" data-bind='fpic' alt="" style='max-height: 80px;'>
                    <?php }else{?>
                    <img src="<?php echo $minfo[fpic]?>" data-bind='fpic' alt="" style='max-height: 80px;display:none'>
                    <?php }?>
                </div>
            </li>
            
            <li>
                <a class="add_address mt10" href="javascript:;">保存</a>
            </li>
        </ul>
        </form>
    </div>

    <div id="footer"></div>


    <script type="text/javascript" src="../../js/template.js"></script>
    <!-- <script type="text/javascript" src="../../js/common.js"></script> -->
    <script type="text/javascript" src="../../js/tmpl/common-top.js"></script>
    <script type="text/javascript" src="../../js/tmpl/footer.js"></script>
</body>
</html>

<script>
        var old_mobile = '<?php echo $minfo['member_mobile']?>';

        $(document).on('change','input[type=file]',function(){
            var file = this.files[0];
            if(!/image\/\w+/.test(file.type)){
                layermsg("请确保文件为图像类型");
                return;
            }
            if(file.size > 1024 * 1024 * 5){
                layermsg('请上传小于5M的图片');
                return;
            }
            var reader = new FileReader();
            var bind = $(this).attr('data-bind');
            reader.onload = function(e){
                var re = /^data:base64,/;
                var ret = this.result + '';
                var type = this.type || file.name.match(/\.([^\.]+)$/i)[1]
                if (re.test(ret)) ret = ret.replace(re, 'data:' + type + ';base64,');
                $('input[name="' + bind + '"]').val(ret);
                console.log(this.result)
                if($('img[data-bind="' + bind + '"]').length){
                    $('img[data-bind="' + bind + '"]').attr('src',this.result).show();
                }
            }
            reader.readAsDataURL(this.files[0]);
        });

        function checkIDCard(idcode){
            var weight_factor = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];
            var check_code = ['1', '0', 'X' , '9', '8', '7', '6', '5', '4', '3', '2'];
            var code = idcode + "";
            var last = idcode[17];//最后一个
            var seventeen = code.substring(0,17);
            var arr = seventeen.split("");
            var len = arr.length;
            var num = 0;
            for(var i = 0; i < len; i++){
                num = num + arr[i] * weight_factor[i];
            }
            var resisue = num%11;
            var last_no = check_code[resisue];
            var idcard_patter = /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/;
            var format = idcard_patter.test(idcode);
            return last === last_no && format ? true : false;
        }

        $('.add_address').click(function(){
            var mobile = $('[name="member_mobile"]').val().trim();
            var member_truename = $('[name="member_truename"]').val().trim()
            var idcard = $('[name="idcard"]').val().trim();

            if(mobile != ''){
                if(!/^1[345789][0-9]{9}$/.test(mobile)){
                    layermsg('请输入正确的手机号码');
                    return false;
                }
            }
            if(checkIDCard(idcard) == false){
                layermsg('请输入正确的身份证号');
                return false;
            }
            if(member_truename != ''){
                if(!/[\u4e00-\u9fa5]/.test(member_truename)){
                    layermsg('请输入仅包含汉字的名字');
                    return false;
                }
            }

            if(mobile != ''){
                if(old_mobile != mobile && $('[name="mobile_code"]').val() == ''){
                    $.get('/shop/index.php?act=seccode&op=send_message',{check_captcha:false,mobile:mobile},function(msg){
                        if(msg == 0x01 << 1){
                            layermsg('手机号码不正确');
                            return false;
                        }
                        if(msg == 0x01 << 2){
                            layermsg('该手机已被绑定');
                            return false;

                        }
                        if(msg == 0x01 << 3){
                            layermsg('短信发送失败');
                        }

                        layermsg('您的手机号码已变更<br>请输入手机验证码');
                        $('#captcha').show();
                    });
                    
                    return false;
                }
            }


            $('form').submit();
        });
    </script>